package inc.zhugastrov.sellerform;

import inc.zhugastrov.ui.AuthorizationForm;
import inc.zhugastrov.users.Position;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class ItemsTableModel extends AbstractTableModel {
	private String[] columnNames = { "ID", "Name", "Brand", "Price", "Amount" };;
	private Vector<Product> dataVector;

	public ItemsTableModel() {
		dataVector = new Vector<Product>();

	}

	public String getColumnName(int column) {
		return columnNames[column];
	}

	public Object getValueAt(int row, int column) {
		Product item = (Product) dataVector.get(row);
		switch (column) {
		case 0:
			return item.getId();
		case 1:
			return item.getName();
		case 2:
			return item.getBrand();
		case 3:
			if (item instanceof Wherehouse)
				return item.getWherehousePrice();
			else
				return item.getPrice();
		case 4:
			return item.getAmountInStore();
		default:
			return null;
		}
	}

	public void setData(Vector<Product> data) {
		dataVector = data;
	}

	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public void changeAmount(int amount, int itemNumber) {
		getRow(itemNumber).setAmountInStore(amount);
	}

	public void changeAmount(int amount, Item item) {
		item.setAmountInStore(amount);
	}

	public Product findWithId(Long id) {
		for (Product item : dataVector) {
			if (item.getId() == id)
				return item;
		}
		return null;
	}

	public int getRowCount() {
		return dataVector.size();
	}

	public Double getTotalPrice() {
		Double totalPrice = 0d;
		for (Product item : dataVector) {
			totalPrice += item.getPrice();
		}
		return totalPrice;
	}

	public void emptyTable() {
		dataVector.removeAllElements();
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
	}

	public Vector<Product> getData() {
		return dataVector;
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public void addRow(Product item) {
		dataVector.add(item);
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);

	}

	public Product getRow(int selectedRow) {
		if (selectedRow <= getRowCount())
			return dataVector.get(selectedRow);
		return null;
	}

	public void deleteRow(int rowNumber) {
		dataVector.remove(rowNumber);
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
	}
}
